package com.netintech.project.system.word.controller;

import com.netintech.common.utils.poi.ExcelUtil;
import com.netintech.core.aspectj.lang.annotation.Log;
import com.netintech.core.aspectj.lang.enums.BusinessType;
import com.netintech.core.web.controller.BaseController;
import com.netintech.core.web.domain.RequestResult;
import com.netintech.core.web.page.TableDataInfo;
import com.netintech.project.system.word.domain.SensitiveWord;
import com.netintech.project.system.word.service.ISensitiveWordService;
import com.netintech.project.system.word.service.impl.SensitiveWordFilterServiceImpl;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * 敏感词Controller
 *
 * @author admin
 * createTime 2020-08-25
 * lastModify admin
 * lastModifyTime 2020-08-25
 * group 电子政务小组
 */
@Controller
@RequestMapping("/system/word")
public class SensitiveWordController extends BaseController
{
    private String prefix = "system/word";

    @Autowired
    private ISensitiveWordService sensitiveWordService;

    @Autowired
    private SensitiveWordFilterServiceImpl sensitiveWordFilterServiceImpl;

    @RequiresPermissions("system:word:view")
    @GetMapping()
    public String word()
    {
        return prefix + "/word";
    }

    /**
     * 查询敏感词列表
     */
    @RequiresPermissions("system:word:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SensitiveWord sensitiveWord)
    {
        startPage();
        List<SensitiveWord> list = sensitiveWordService.selectSensitiveWordList(sensitiveWord);
        return getDataTable(list);
    }

    /**
     * 导出敏感词列表
     */
    @RequiresPermissions("system:word:export")
    @PostMapping("/export")
    @ResponseBody
    public RequestResult export(SensitiveWord sensitiveWord)
    {
        List<SensitiveWord> list = sensitiveWordService.selectSensitiveWordList(sensitiveWord);
        ExcelUtil<SensitiveWord> util = new ExcelUtil<SensitiveWord>(SensitiveWord.class);
        return util.exportExcel(list, "word");
    }

    /**
     * 新增敏感词
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存敏感词
     */
    @RequiresPermissions("system:word:add")
    @Log(title = "敏感词", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public RequestResult addSave(SensitiveWord sensitiveWord)
    {
        Set<String> set = sensitiveWordFilterServiceImpl.getSensitiveWord(sensitiveWord.getWord(), 2);
        if(set !=null && set.size()>0){
            return RequestResult.error("文案内容包含"+set.size()+"个敏感词汇："+set);
        }
        sensitiveWord.setCreateDate(new Date());
        sensitiveWord.setCreateUser(getSysUser().getUserId());
        return toAjax(sensitiveWordService.insertSensitiveWord(sensitiveWord));
    }

    /**
     * 修改敏感词
     */
    @GetMapping("/edit/{sensitiveWordId}")
    public String edit(@PathVariable("sensitiveWordId") Long sensitiveWordId, ModelMap mmap)
    {
        SensitiveWord sensitiveWord = sensitiveWordService.selectSensitiveWordById(sensitiveWordId);
        mmap.put("sensitiveWord", sensitiveWord);
        return prefix + "/edit";
    }

    /**
     * 修改保存敏感词
     */
    @RequiresPermissions("system:word:edit")
    @Log(title = "敏感词", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public RequestResult editSave(SensitiveWord sensitiveWord)
    {
        sensitiveWord.setUpdateDate(new Date());
        return toAjax(sensitiveWordService.updateSensitiveWord(sensitiveWord));
    }

    /**
     * 查看敏感词
     */
    @RequiresPermissions("system:word:detail")
    @GetMapping("/view/{sensitiveWordId}")
    public String view(@PathVariable("sensitiveWordId") Long sensitiveWordId, ModelMap mmap)
    {
        SensitiveWord sensitiveWord = sensitiveWordService.selectSensitiveWordById(sensitiveWordId);
        mmap.put("sensitiveWord", sensitiveWord);
        return prefix + "/view";
    }
    /**
     * 删除敏感词
     */
    @RequiresPermissions("system:word:remove")
    @Log(title = "敏感词", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public RequestResult remove(String ids)
    {
        return toAjax(sensitiveWordService.deleteSensitiveWordByIds(ids));
    }
}
